package com.html;

import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.model.xmlElement.DataColumn;
import com.model.xmlElement.DataColumnPattern;
import com.tool.StringUtil;

import constant.HtmlConstant;
/**
 * *********************************************
 * @author Administrator
 * @FileName ParseW3CNodeArrayWithPattern.java
 * @Description解析org.w3c.dom.Node[](带pattern)
 **********************************************
 */
public class ParseW3CNodeArrayWithPattern extends ParseW3CNodeArray{
	public static final Logger LOG = LoggerFactory.getLogger(ParseW3CNodeArrayWithPattern.class);
	
	private ParseHtml parseHtml;
	private DataColumn dataColumn;
	private Node[] nodes;
	
	
	public ParseHtml getParseHtml() {
		return parseHtml;
	}
	public void setParseHtml(ParseHtml parseHtml) {
		this.parseHtml = parseHtml;
	}
	public DataColumn getDataColumn() {
		return dataColumn;
	}
	public void setDataColumn(DataColumn dataColumn) {
		this.dataColumn = dataColumn;
	}
	public Node[] getNodes() {
		return nodes;
	}
	public void setNodes(Node[] nodes) {
		this.nodes = nodes;
	}
	public ParseW3CNodeArrayWithPattern(){}
	public ParseW3CNodeArrayWithPattern(ParseHtml parseHtml, DataColumn dataColumn, Node[] nodes) {
		super();
		this.parseHtml = parseHtml;
		this.dataColumn = dataColumn;
		this.nodes = nodes;
	}
	
	private NodeList getPatternNode(String xpath,int index){
		NodeList nodeList = null;
		Object result = null;
		try {
			result = parseHtml.getxPath().evaluate(xpath, parseHtml.getDom(), XPathConstants.NODESET);
		}  catch (XPathExpressionException e) {
			e.printStackTrace();
		}
		if (result instanceof NodeList) {
			nodeList = (NodeList) result;
		}
		return nodeList;
	}
	
	
	public String parse(){
		String tagNodeValue = "";
		boolean needPattern = false;
		DataColumnPattern dataColumnPattern = dataColumn.getDataColumnPattern();
		if(dataColumnPattern != null && dataColumnPattern.getPatternXpath() != null && !"".equals(dataColumnPattern.getPatternXpath())){
			needPattern = true;
		}
		
		StringBuilder stringBuilder = new StringBuilder(100);
		boolean contains = false;
		Node node;
		int index = 0;
		for(int i = 0,length = nodes.length;i < length;i++){
			node = nodes[i];
			if(needPattern){
				if(node.getTextContent().toString().contains(dataColumn.getDataColumnInput().getName())){
					index += i + 1;
					contains = true;
					break;
				}
			}
		}
		String xpath = dataColumn.getDataColumnPattern().getPatternXpath();
		String tmp = xpath.substring(xpath.indexOf("${") + 2, xpath.lastIndexOf("}"));
		String[] tmps = tmp.split(",");
		int start = 0;
		for(int i = 0,len = tmps.length;i < len;i++){
			if(!tmps[i].equals("index")){
				index += Integer.valueOf(tmps[i]);
				start = Integer.valueOf(tmps[i]);
				break;
			}
		}
		xpath = dataColumn.getDataColumnPattern().getPatternXpath().replace("${" + tmp + "}", String.valueOf(index));
		if(contains){
			if(index > start){
				String outPutType = dataColumn.getDataColumnOutput().getOutputType();
				NodeList nodeList = getPatternNode(xpath,index);
				int resultLength = nodeList.getLength();
				for (int i = 0; i < resultLength; i++) {
					node = nodeList.item(i);
					if(outPutType.equals(HtmlConstant.OUTPUTTYPE_TEXT)){
						stringBuilder.append(node.getTextContent().toString());
					}else if(outPutType.equals(HtmlConstant.OUTPUTTYPE_HTML)){
						stringBuilder.append(ParseImgs.parse(parseHtml.getSeed(), nodeToHtml(node)));
					}
				}
				tagNodeValue = StringUtil.parseContent(stringBuilder.toString());
				if(tagNodeValue != null && !"".equals(tagNodeValue)){
					tagNodeValue = tagNodeValue.replace("\"", "'");
				}
			}
		}
		return tagNodeValue;
	}

}
